<template>
  <view class="page container">
    <u-form
      :model="basicForm"
      :rules="rules"
      labelPosition="top"
      ref="form"
      labelWidth="auto"
      :labelStyle="{
        fontSize: '14px',
        color: '#B2B2B2',
        lineHight: '20px',
      }"
    >
			<h3 style="display: flex;align-items: center;justify-content: center; margin-bottom: 20rpx;">款项进度</h3>
      <view style="max-height: 80vh; overflow-y: auto;">
      	<view class="basic-style" >
      	  <u-form-item label="款项名称" prop="nodeName">
      	    <u--input disabled placeholder="请输入款项名称" v-model="basicForm.nodeName"></u--input>
      	  </u-form-item>
      	  <u-form-item label="金额(元)" prop="paymentMoney">
      	    <u--input disabled type="number" v-number-normal placeholder="请输入金额" v-model="basicForm.paymentMoney"></u--input>
      	  </u-form-item>
      	  <u-form-item label="款项节点说明" prop="nodeDesc">
      	    <u--textarea
      	      disabled
      	      maxlength="500"
      	      confirm-type="send"
      	      v-model="basicForm.nodeDesc"
      	      placeholder="请输入款项节点说明"
      	    ></u--textarea>
      	  </u-form-item>
      	  <!-- <u-form-item label="是否关联任务" prop="isRelatedTask" required>
      			<h-dict-radio v-model="basicForm.isRelatedTask" :disabled="relatedTaskDisabled" dictName="YES_OR_NO" @input="relatedTaskInput"></h-dict-radio>
      	  </u-form-item> -->
      	  <!-- <template v-if="basicForm.isRelatedTask == 1">
      	    <u-form-item label="是否关联进度" prop="isRelatedTaskNode" required>
      				<h-dict-radio v-model="basicForm.isRelatedTaskNode" :disabled="isRelatedTaskNodeDisabled" dictName="YES_OR_NO" @input="collectionDateInput"></h-dict-radio>
      	    </u-form-item>
      	  </template> -->
      	  <!-- <template v-if="basicForm.isRelatedTaskNode == 1">
      	    
      	  </template> -->
					<u-form-item label="任务进度" prop="taskNodeId" required>
					  <select-task-progress-input
					    style="width: 100%"
					    v-model="basicForm.taskNodeId"
					    :progressVoList="progressVoList"
					    objectField="basicForm.templateId"
					    placeholder="请选择任务进度"
					    border="surround"
					    suffixIcon="arrow-down-fill"
					    :suffixIconStyle="{ color: '#B2B2B2' }"
					    @detail="onChangeTaskNodeId"
					    @change="onProgressChange"
					  ></select-task-progress-input>
					</u-form-item>
					<u-form-item label="本任务关联款项金额(元)" prop="taskPaymentMoney" :borderBottom="false">
					  <u--input v-model="basicForm.taskPaymentMoney" placeholder="请输入本任务关联款项金额" border="surround" disabled type="number"></u--input>
					</u-form-item>
      	  <!-- <u-form-item label="是否应收" prop="isCollectionPayment" required v-if="basicForm.isRelatedTask == 1">
      			<h-dict-radio v-model="basicForm.isCollectionPayment" dictName="YES_OR_NO"  @input="isCollectionPaymentInput"></h-dict-radio>
      	  </u-form-item> -->
      	  <!-- <template v-if="basicForm.isCollectionPayment == 1">
      	    <u-form-item label="预计应收日期" prop="collectionDate" required>
      	      <h-time-select
      	        mode="date"
      	        v-model="basicForm.collectionDate"
      	        placeholder="请选择预计应收日期"
      	        border="surround"
      	        suffixIcon="calendar-fill"
      	        :suffixIconStyle="{ color: '#B2B2B2' }"
      	      ></h-time-select>
      	    </u-form-item>
      	  </template> -->
      	</view>
      </view>

      <view class="space-x-1 mt-1" style="display: flex;">
        <u-button type="primary" text="关闭" :loading="loading" plain @click="onClose"></u-button>
				<u-button type="primary" text="保存" :loading="loading" @click="onSubmit"></u-button>
      </view>
    </u-form>
  </view>
</template>

<script>
import http from '@/utils/request/index'
import selectTaskProgressInput from '@/pages/synergy/components/selectTaskProgressInput.vue'

export default {
  name: 'PriceForm',
  components: {
    selectTaskProgressInput,
  },
  props: {
    teamId: {
      type: [Number, String],
      default: '',
    },
    companyId: {
      type: [Number, String],
      default: '',
    },
    // 表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    datailDisabled: {
      type: Boolean,
      default: false,
    },
    index: {
      type: Number,
      default: null,
    },
		progressVoList: {
			type: Array,
			default: () => [],
		}
  },
  data() {
    return {
      smallList: [],
      loading: false,
      basicForm: {
        nodeName: '',
        paymentMoney: '',
        nodeDesc: '',
        // isRelatedTask: '',
        // isRelatedTaskNode: '',
        taskNodeId: '',
				taskPaymentMoney: '',
				taskProgress: ''
        // isCollectionPayment: '',
        // collectionDate: '',
      },
      rules: {
        // isRelatedTask: [{ required: true, message: '请选择是否关联任务', trigger: ['change', 'blur'] }],
        // isRelatedTaskNode: [{ required: true, message: '请选择是否关联任务进度', trigger: ['change', 'blur'] }],
        // collectionDate: [{ required: true, message: '请选择预计应收日期', trigger: ['change', 'blur'] }],
        taskNodeId: [{ required: true, message: '请选择关联任务进度', trigger: ['change', 'blur'] }],
        // isCollectionPayment: [{ required: true, message: '请选择是否应收', trigger: ['change', 'blur'] }],
      },
    }
  },
  created() {},
  computed: {
    laborCost() {
      return (parseFloat(this.basicForm.dayCost) || 0) * (this.basicForm.expectWorkTime || 0) + (this.basicForm.projectPerformance || 0)
    },
    relatedTaskDisabled() {
      return this.basicForm.taskCompleteStatus == 2 && this.basicForm.isRelatedTask == 1
    },
    isRelatedTaskNodeDisabled() {
      return this.basicForm.taskCompleteStatus == 2 && this.basicForm.isRelatedTaskNode == 1
    },
  },
  methods: {
    
    productTeamChange(val) {
      // debugger;
      http.get('/api/erp/omc/teammanager/erpOmcTeamManager/queryById', { id: val }).then((res) => {
        if (res.success) {
          // debugger;
          let data = res.result.records || res.result
          this.$set(this.basicForm, 'productTeam', val)
          this.$set(this.basicForm, 'teamLeader', data.tdfzr)
        }
      })
    },
    handleRateChange(e) {
      if (this.assignedAmount !== 0) {
        let amount = (this.assignedAmount * e) / 100
        this.basicForm.contractAmount = Math.round((amount + Number.EPSILON) * 100) / 100
      } else {
        this.basicForm.contractAmount = 0
      }
    },
    handleAmountChange(e) {
      if (this.assignedAmount !== 0) {
        let rate = (e / this.assignedAmount) * 100
        this.basicForm.contractRate = Math.round((rate + Number.EPSILON) * 100) / 100
      } else {
        this.basicForm.contractRate = 0
      }
    },
    businessCategoryHandle(obj) {
      if (obj) {
        this.basicForm.categoryName = obj.categoryName
      }
    },

    // 请选择是否关联任务
    relatedTaskInput(item) {
      this.$set(this.basicForm, 'isRelatedTaskNode', '0')
      this.$set(this.basicForm, 'isCollectionPayment', '0')
      this.$set(this.basicForm, 'taskNodeId', null)
      this.$set(this.basicForm, 'taskNodeId_dictText', null)
    },

    // 请选择是否关联任务进度
    collectionDateInput(item) {
      this.$set(this.basicForm, 'taskNodeId', null)
      this.$set(this.basicForm, 'taskNodeId_dictText', null)
      if (item == 0) {
        this.$delete(this.rules, 'taskNodeId')
      } else {
        this.$set(this.rules, 'taskNodeId', [{ required: true, message: '请选择关联任务进度!', trigger: ['change', 'blur'] }])
      }
    },
		
		// 请选择是否应收
		isCollectionPaymentInput (item) {
			this.$set(this.basicForm, 'collectionDate', '')
			if (item == 0) {
			  this.$delete(this.rules, 'collectionDate')
			} else {
			  this.$set(this.rules, 'collectionDate', [{ required: true, message: '请选择应收日期!', trigger: ['change', 'blur'] }])
			}
		},

    // 选择任务进度
    onChangeTaskNodeId(item) {
      if (item && item.length && item[0].nodeName) {
        this.$set(this.basicForm, 'taskNodeId_dictText', item[0].nodeName)
      }
    },
		onProgressChange(item) {
		  // this.basicForm.nodeName = item.nodeName
		  this.basicForm.taskNodeId_dictText = item && item.nodeName? item.nodeName : ''
			this.$set(this.basicForm, 'taskProgress', item.nodeProgress || '') // 任务节点进度
		},
    reset() {
      this.$refs.form.resetFields()
    },
    async onSubmit() {
      await this.$refs.form.validate()
      if (!this.index) {
        this.$emit('submit', [
          {
            id: parseInt(Math.random() * 1000000000000),
            ...this.basicForm,
          },
          this.index,
        ])
      } else {
        this.$emit('submit', [this.basicForm, this.index])
      }
    },
    onClose() {
      this.$emit('close')
    },
  },
  watch: {
    laborCost(val) {
      this.basicForm.laborCost = val
    },
  },
}
</script>

<style scoped></style>
